package com.szqhyxj.youxiangju.biz.partnerHome;

import com.szqhyxj.youxiangju.BaseConsumer;
import com.szqhyxj.youxiangju.BasePresenterImpl;
import com.szqhyxj.youxiangju.biz.core.TokenManager;
import com.szqhyxj.youxiangju.biz.core.UserInfo;
import com.szqhyxj.youxiangju.biz.core.UserInfoManger;
import com.szqhyxj.youxiangju.biz.core.UserInfoModel;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by leung on 2018/10/30.
 */

public class PartnerContainerPresenter extends BasePresenterImpl implements PartnerFragmentsContract.Presenter {

    private PartnerFragmentsContract.View view;

    private List<Disposable> disposables = new ArrayList<>();

    PartnerContainerPresenter(PartnerFragmentsContract.View view) {
        this.view = view;
    }

    @Override
    public void onDestroy() {
        for (Disposable disposable : disposables) {
            if (disposable != null) {
                disposable.dispose();
            }
        }
    }

    @Override
    public void setHome() {
        this.view.showHome();
    }

    @Override
    public void setShop() {
        if (preCheck()) {
            return;
        }
        view.showShop();
    }

    @Override
    public void setPlan() {
        if (preCheck()) {
            return;
        }
        view.showPerformance();
    }

    @Override
    public void setMy() {
        if (!TokenManager.INSTANCE.online()) {
            view.navToLogin();
            return;

        }
        this.view.showMy();
    }

    private boolean preCheck() {
        if (!TokenManager.INSTANCE.online()) {
          view.navToLogin();
            return true;
        }

        return false;
    }

    private static final String TAG = PartnerContainerPresenter.class.getSimpleName();

    @Override
    public void init() {

        if (!TokenManager.INSTANCE.online()) {
            view.showAll();
            view.showHome();
            return;
        }
        startRequest(UserInfoModel
                .loadUserInfo()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(
                        new BaseConsumer<UserInfo>() {
                            @Override
                            protected void error(String code, String msg){

                            }

                            @Override
                            protected void successAccept(UserInfo userInfo) {
                                UserInfoManger.INSTANCE.init(userInfo);
                                view.showAll();
                                view.showHome();
                            }
                        }));
    }
}
